import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    access_token: localStorage.getItem('access_token') || '',
    refresh_token: localStorage.getItem('refresh_token') || '',
    username: localStorage.getItem('username') || '' // 存储 username
  },
  mutations: {
    setAccessToken (state, token) {
      state.access_token = token
    },
    setRefreshToken (state, token) {
      state.refresh_token = token
    },
    setUsername (state, username) { // 添加 setUsername mutation
      state.username = username
    }
  },
  actions: {
    // 你可以添加一些需要异步处理的 action
    setAccessToken ({ commit }, token) {
      commit('setAccessToken', token)
    },
    setRefreshToken ({ commit }, token) {
      commit('setRefreshToken', token)
    },
    setUsername ({ commit }, username) {
      commit('setUsername', username)
    }
  },
  getters: {
    getAccessToken: (state) => state.access_token,
    getRefreshToken: (state) => state.refresh_token,
    username: (state) => state.username // 添加 username getter
  }
})
